<?php
    /*
        AplCategoryCombo 
        Part of Another-Php-Library
                
        This component will display a category hierarchy via apl_html_selection 
        object.Category table must have at least these 3 fields 
        Category_Id,Category_NameParent_Category
        
        Combo Contents Will be like this ;)        
        
        +Category_1
        |--SubCategory_1
        |  |--SubSubCategory_1
        |--SubCategory_2
        +Category_2
        |--SubCategory_2        
    */
    
    /**
     * @ignore
     */
    if( defined("apl_".__FILE__)) return;
    define("apl_".__FILE__,"apl_".__FILE__); 
       
    require_once(APL_ROOT."/db/apl_db.php");
    require_once(APL_ROOT."/html/apl_html_selection.php");
        
    class AplCategoryCombo{
        
        private $fldId;
        private $fldName;
        private $fldParent;
        private $tblName;
        private $qryObject;                
        private $selObject;
            
        public function setFieldNames( $id, $name, $parent, $tableName )
        {
            $this->fldId        = $id;
            $this->fldName      = $name;
            $this->fldParent    = $parent;                        
            $this->tblName      = $tableName;
        }
        
        public function setConnectionObject(  AplConnection $obj )
        {
            $this->qryObject = new AplQuery( $obj );            
        }
        
        public function setSelectionObject( AplHtmlSelection $obj )
        {
            $this->selObject = $obj;
        }
        
        public function getCategoryList( $parentCat, $depth )
        {
            $fncResult = array("" => "1");
            $mainQry = $this->qryObject->derive();            
            $subQry = $this->qryObject->derive();
            
            $sql = "select ".
                    $this->fldId.",".
                    $this->fldName.",".
                    $this->fldParent.
                    " from ".$this->tblName." where ".
                    $this->fldParent."= $parentCat order by ".
                    $this->fldName;            
            $mainQry->open( $sql,3000 );
            do{
                $indent = "";
                for( $i=1;$i<$depth;$i++)
                    $indent.="&nbsp;&nbsp;";
                
                $catRow = $mainQry->fetch();
                $fncResult[$indent.$catRow[$this->fldName]] = $catRow[$this->fldId];                                
                
                $sql =  "Select Count(*) SubCount From ".
                        $this->tblName." where ".
                        $this->fldParent." = ".$catRow[$this->fldId];                
                        
                $subQry->open( $sql );
                $subCount = $subQry->fetch();
                if( $subCount["SubCount"]>0 )
                {                    
                    $subResult = $this->getCategoryList( $catRow[$this->fldId ], $depth+1 );
                    $fncResult = array_merge($fncResult,$subResult );
                }
                
            }while( $mainQry->nextRecord());
            return $fncResult;            
        }
        
        public function fill( $rootCategory )
        {
            $categories = $this->getCategoryList( $rootCategory, 1 );
            $this->selObject->clear();
            foreach( $categories as  $label => $id )
                $this->selObject->addPair( $id, $label );
        }
        
        public function setSelection( $selectionValue )
        {
            $this->selObject->SelectedIndex = $selectionValue;
        }
                               
        public function buildTag()
        {
            return $this->selObject->buildTag();
        }
    }

  
 
?> 